package com.means.polymerize.core.terminal.common.lang;

import com.jcraft.jsch.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 通道存储容器
 *
 * @author xuhaifeng
 * @since 2021.10.30
 */
@Slf4j
public class ChannelStorageContainer {
    /**
     * 通道容器
     * <br/>
     * 每个连接的操作者会分配一个Channel容器
     */
    private static final ConcurrentHashMap<String, Channel> SENDER_MAP = new ConcurrentHashMap<>();

    /**
     * Gets sessions.
     *
     * @return the sessions
     */
    public static Channel getChannel(String token) {
        if (SENDER_MAP.containsKey(token)) {
            return SENDER_MAP.get(token);
        }
        throw new RuntimeException("通道不存在！！");
    }

    /**
     * Sets sessions.
     *
     * @param token  the token
     * @param sender the sender
     */
    public static void setChannel(String token, Channel sender) {
        SENDER_MAP.put(token, sender);
    }

    public static void removeChannel(String token) {
        SENDER_MAP.remove(token);
    }
}
